import agconnect from '@hw-agconnect/api-ohos';
import "@hw-agconnect/function-ohos";
import { Constants } from '../common/Constants';
import { Log } from '../common/Log';
import { getAGConnect } from './AgcConfig';

export function getSudokuPuzzle(context: any, level: number): Promise<string> {
  return new Promise((resolve, reject) => {
    getAGConnect(context);
    // 调用wrap方法设置函数，在方法中传入触发器标识，返回得到可执行云函数对象
    let functionCallable = agconnect.function().wrap("sudoku-algorithm-$latest");
    // 调用call方法运行云函数，若函数有入参，可以将参数转化为JSON对象或JSON字符串传入，若无参则不传
    functionCallable.call({ "level": level }).then((ret: any) => {
      // 可调用getValue方法获取函数的返回值
      let result: ApiResult<SudokuPuzzle> = ret.getValue();
      Log.info(Constants.LOG_TAG_NAME, `sudoku-algorithm called,  code: ${result.code}`);
      Log.info(Constants.LOG_TAG_NAME, `sudoku-algorithm called,  desc: ${result.desc}`);
      Log.info(Constants.LOG_TAG_NAME, `sudoku-algorithm called,  data: ${result.data}`);
      let data: SudokuPuzzle = result.data;
      resolve(JSON.stringify(data));
    }).catch((err: any) => {
      Log.error(Constants.LOG_TAG_NAME, `sudoku-algorithm failed, cause: ${JSON.stringify(err)}`);
    })
  });
}

export class ApiResult<T> {
  code: number;
  desc: string;
  data: T;

  constructor(code: number, desc: string, data: T) {
    this.code = code;
    this.desc = desc;
    this.data = data;
  }
}

export class SudokuPuzzle {
  original: Array<Array<number>>;
  answer: Array<Array<Array<number>>>;

  constructor(original: Array<Array<number>>, answer: Array<Array<Array<number>>>) {
    this.original = original;
    this.answer = answer;
  }
}